﻿#include "DrawColumn.h"
#include <QtGui>
DrawColumn::DrawColumn(QWidget *parent)
    : QWidget(parent)
{
    ui.setupUi(this);
}

DrawColumn::~DrawColumn()
{
}

void DrawColumn::paintEvent(QPaintEvent * p)
{
    QPainter painter(this);
    int nWidth = this->width();
    int nHeight = this->height();
    painter.fillRect(0, 0, nWidth, nHeight, QColor(0, 0, 0));
    if (m_iType == 0)
    {
        QPen pen(QColor(0, 150, 0), 1.0);
        painter.setPen(pen);

        int nlx1 = nWidth / 2 - 2 - 33, nlx2 = nWidth / 2 - 2, nly = nHeight / 2 - 4 + 5 * 2;
        int nrx1 = nWidth / 2 + 2, nrx2 = nWidth / 2 + 2 + 33, nry = nHeight / 2 - 4 + 5 * 2;
        for (int i = 0; i < 10; i++)
        {
            painter.drawLine(nlx1, nly - i * (m_iSpace * 2), nlx2, nly - i * (m_iSpace * 2));
            painter.drawLine(nrx1, nry - i * (m_iSpace * 2), nrx2, nry - i * (m_iSpace * 2));
        }
        painter.setPen(QPen(Qt::green, 1.0));
        QString snum = QString::number(m_iNum) + "%";
        painter.drawText(nWidth / 2 - snum.size() / 2, nHeight - 2, snum);
        int count = m_iNum / 10;
        for (int i = 0; i < count; i++)
        {
            painter.drawLine(nlx1, nly - i * (m_iSpace * 2), nlx2, nly - i * (m_iSpace * 2));
            painter.drawLine(nrx1, nry - i * (m_iSpace * 2), nrx2, nry - i * (m_iSpace * 2));
        }
    }
    else if(m_iType==1)
    {
        QPen pen(QColor(0, 150, 0), 3.0);
        painter.setPen(pen);

        int nDrawWidth = (nWidth - 30)/2;
        int nDrawHeight = (nHeight - 30)/10;

        int nY = nHeight / 2 - nDrawHeight + 5 * nDrawHeight;

        int nlx1 = nWidth / 2 - nDrawWidth - 3, nlx2 = nWidth / 2 - 3;
        int nrx1 = nWidth / 2 + 3, nrx2 = nWidth / 2 + nDrawWidth + 3;
        for (int i = 0; i < 10; i++)
        {
            painter.drawLine(nlx1, nY - nDrawHeight*i, nlx2, nY-nDrawHeight*i);
            painter.drawLine(nrx1, nY - nDrawHeight*i, nrx2, nY-nDrawHeight*i);
        }
        painter.setPen(QPen(Qt::green, 3.0));
        painter.setFont(QFont("SimSun",16));
        QString snum = QString::number(m_iNum) + "%";
        painter.drawText(nWidth / 2 - snum.size()*10 / 2, nHeight-nDrawHeight/2, snum);
        int count = m_iNum / 10;
        for (int i = 0; i < count; i++)
        {
            painter.drawLine(nlx1, nY - nDrawHeight*i, nlx2, nY - nDrawHeight*i);
            painter.drawLine(nrx1, nY - nDrawHeight*i, nrx2, nY - nDrawHeight*i);
        }
    }
}

void DrawColumn::setNum(int n)
{
    m_iNum = n;
    update();
}

void DrawColumn::setType(int type)
{
    m_iType = type;
    m_iSpace = 2;
}
